﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Maticsoft.Common;

namespace Maticsoft.Web.Admin.Link
{
    public partial class edit : PageBase
    {
        string act = string.Empty;
        string id = string.Empty;

        BLL.Links bll = new BLL.Links();
        Model.Links model = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            Check("Link", "modify");

            act = Request.QueryString["act"];
            id = Request.QueryString["id"];
            if (string.IsNullOrEmpty(act))
            {
                MessageBox.ShowAndRedirect(this, "参数错误", "list.aspx");
            }
            if (!string.IsNullOrEmpty(id))
            {
                if (!IsPostBack)
                {
                    GetData();
                }
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (act == "add")
            {
                model = new Model.Links();
                model.Title = txtTitle.Text.Trim();
                model.Type = int.Parse(ddlType.SelectedValue);
                model.Url = txtUrl.Text.Trim();
                ImageUpload img = new ImageUpload();
                img.SavePath = "~/uploadfiles/links/";
                img.FormFile = File1;
                img.Upload();
                if (img.Error == 0)
                {
                    model.Pic = img.OutFileName;
                }
                model.OrderNo = int.Parse(txtOrderNo.Text.Trim());
                model.CreateTime = DateTime.Now;
                model.UpdateTime = DateTime.Now;
                model.IsActive = 1;
                bll.Add(model);

                MessageBox.ShowAndRedirect(this, "添加成功", "list.aspx");
            }
            if (act == "edit")
            {
                model = bll.GetModel(int.Parse(id));
                model.Title = txtTitle.Text.Trim();
                model.Type = int.Parse(ddlType.SelectedValue);
                model.Url = txtUrl.Text.Trim();
                ImageUpload img = new ImageUpload();
                img.SavePath = "~/uploadfiles/links/";
                img.FormFile = File1;
                img.Upload();
                if (img.Error == 0)
                {
                    model.Pic = img.OutFileName;
                }
                model.OrderNo = int.Parse(txtOrderNo.Text.Trim());
                model.UpdateTime = DateTime.Now;
                model.IsActive = 1;
                bll.Update(model);

                MessageBox.ShowAndRedirect(this, "修改成功", "list.aspx");
            }
        }

        private void GetData()
        {
            model = bll.GetModel(int.Parse(id));
            txtTitle.Text = model.Title;
            if (!string.IsNullOrEmpty(model.Pic))
            {
                Image1.ImageUrl = "~/uploadfiles/links/" + model.Pic;
            }
            ddlType.SelectedValue = model.Type.ToString();
            txtOrderNo.Text = model.OrderNo.ToString();
            txtUrl.Text = model.Url;
        }

    }
}